// dictation-result.js - 听写结果组件
const app = getApp();

Component({
  data: {
    totalWordsCount: 0,
    correctCount: 0,
    wrongCount: 0,
    answeringMode: 'online',
    wordsForDictation: []
  },
  lifetimes: {
    attached() {
      // 获取DictationManager实例
      this.dictationManager = app.getDictationManager();
      
      // 添加状态变化监听器
      this.stateChangeListener = this.onDictationStateChange.bind(this);
      this.dictationManager.addListener(this.stateChangeListener);
      
      // 初始化组件数据
      this.updateComponentData();
    },
    detached() {
      // 移除状态变化监听器
      if (this.stateChangeListener) {
        this.dictationManager.removeListener(this.stateChangeListener);
      }
    }
  },
  methods: {
    /**
     * 监听DictationManager状态变化
     */
    onDictationStateChange(state) {
      this.setData({
        totalWordsCount: state.totalWordsCount,
        correctCount: state.correctCount,
        wrongCount: state.wrongCount,
        answeringMode: state.answeringMode,
        wordsForDictation: state.wordsForDictation
      });
    },
    
    /**
     * 更新组件数据
     */
    updateComponentData() {
      const state = this.dictationManager.getState();
      this.setData({
        totalWordsCount: state.totalWordsCount,
        correctCount: state.correctCount,
        wrongCount: state.wrongCount,
        answeringMode: state.answeringMode,
        wordsForDictation: state.wordsForDictation
      });
    },

    /**
     * 重新开始听写
     */
    restartDictation() {
      // 直接调用管理器的方法重新开始听写
      this.dictationManager.restartDictation();
    },

    /**
     * 重新选择词库和分组
     */
    reselectSource() {
      // 直接调用管理器的方法重新选择词库和分组
      this.dictationManager.reselectSource();
    }
  }
});